package testing.domain.question.dao;

import testing.dataBase.DataBaseConnectionPool;
import testing.domain.question.Question;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class QuestionDao {
     public int createQuestion (Question question) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            prepStat = conn.prepareStatement("INSERT INTO QUESTION (QUESTION_ID,NAME,ANSWERS,RIGHTANSWER) VALUES ( ?, ?, ?, ?);");
            //
            ResultSet resultSet = prepStat.executeQuery("select * from QUESTION");
            int colCount = resultSet.getMetaData().getColumnCount();
            //
            colCount++;
            prepStat.setInt(1, colCount);
            prepStat.setString(2, question.getName());
            prepStat.setString(3, Arrays.toString(question.getAnswers()));
            prepStat.setString(4, question.getRightAnswer());

            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
    }
    public java.util.Collection<Question> getAllQuestions() throws SQLException {
        List<Question> list = new ArrayList<Question>();
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM QUESTION;");
            while(resultSet.next())
            {
                list.add(
                        new Question(
                            resultSet.getInt("QUESTION_ID"),
                            resultSet.getString("NAME"),
                            resultSet.getString("RIGHTANSWER"),
                            resultSet.getString("ANSWERS").trim().split(", "))
                );
            }
            }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
        return list;

    }
     public int editQuestion(Question question) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            prepStat = conn.prepareStatement("UPDATE QUESTION SET NAME = ?, ANSWERS = ?, RIGHTANSWER = ? WHERE QUESTION_ID == ?;");
            prepStat.setString(1, question.getName());
            prepStat.setString(2, Arrays.toString(question.getAnswers()));
            prepStat.setString(3, question.getRightAnswer());
            prepStat.setInt(4, question.getQuestionId());

            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }
    }
     public int deleteQuestion(Question question) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        try {
            conn = DataBaseConnectionPool.getConnection();
            prepStat = conn.prepareStatement("DELETE FROM QUESTION WHERE QUESTION_ID = ?");
            prepStat.setInt(1, question.getQuestionId());
            return prepStat.executeUpdate();
        }finally {
            DataBaseConnectionPool.silentClose(prepStat);
            DataBaseConnectionPool.silentClose(conn);
        }

     }
}
